//
//  KeleDataManager.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/11.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "KeleDataManager.h"
#import <CommonCrypto/CommonDigest.h>
#import <objc/runtime.h>

@implementation KeleDataManager






#pragma mark - 字典转json
+ (NSString*)transformJSON:(NSDictionary*)params
{
    NSData* data = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
+ (NSString*)dictionaryToJson:(NSDictionary *)dic
{
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:kNilOptions error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

#pragma mark - 密码加密方法
+ (NSString*)encryptPsw:(NSString*)psw
{
    NSString* signString = [psw stringByAppendingString:@"cv34fsVKdjh*#HJkd;782313sab4Fb$2"];
    NSString *signMD5Str = [self md5HexDigest:signString];
    return signMD5Str;
}

#pragma mark - input加密方法
+ (NSString*)encrypt:(NSDictionary*)input
{
    NSString *inputString = [self dictionaryToJson:input];
    NSString *signString = [inputString stringByAppendingString:@"xxxxccfdffssewewrwerwercvaxwerwe"];
    NSString *signMD5Str = [self md5HexDigest:signString];
    return signMD5Str;
}

#pragma mark - MD5加密
+ (NSString*)md5HexDigest:(NSString*)string
{
    const char *original_str = [string UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(original_str, (CC_LONG)strlen(original_str), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < 16; i++){
        [hash appendFormat:@"%02X", result[i]];
    }
    return [hash lowercaseString];
}

#pragma mark - runtime获取点电量
+ (NSInteger)getCurrentBatteryLevel
{
    UIApplication *app = [UIApplication sharedApplication];
    if (app.applicationState == UIApplicationStateActive||app.applicationState==UIApplicationStateInactive) {
        Ivar ivar=  class_getInstanceVariable([app class],"_statusBar");
        id status  = object_getIvar(app, ivar);
        for (id aview in [status subviews]) {
            NSInteger batteryLevel = 0;
            for (id bview in [aview subviews]) {
                if ([NSStringFromClass([bview class]) caseInsensitiveCompare:@"UIStatusBarBatteryItemView"] == NSOrderedSame&&[[[UIDevice currentDevice] systemVersion] floatValue] >=6.0)
                {
                    
                    Ivar ivar=  class_getInstanceVariable([bview class],"_capacity");
                    if(ivar)
                    {
                        batteryLevel = ((int (*)(id, Ivar))object_getIvar)(bview, ivar);
                        //这种方式也可以
                        /*ptrdiff_t offset = ivar_getOffset(ivar);
                         unsigned char *stuffBytes = (unsigned char *)(__bridge void *)bview;
                         batteryLevel = * ((int *)(stuffBytes + offset));*/
                        if (batteryLevel > 0 && batteryLevel <= 100) {
                            return batteryLevel;
                            
                        } else {
                            return 0;
                        }
                    }
                    
                }
                
            }
        }
    }
    
    return 0;
}

@end
